package com.question_barrier.service.impl;

import com.question_barrier.entity.Barrier;
import com.question_barrier.repository.BarrierRepository;
import com.question_barrier.result.StateCode;
import com.question_barrier.service.BarrierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class BarrierServiceImpl implements BarrierService {

    @Autowired
    BarrierRepository barrierRepository;

    @Override
    public List<Barrier> getAllBarrier()
    {
        List<Barrier> barriers;
        barriers = barrierRepository.findAll();
        return barriers;
    }

    @Override
    public List<Barrier>  findByGradeAndSubject(String grade,String subject) {
        return barrierRepository.findByGradeAndSubject(grade,subject);
    }

    @Override
    public Barrier findById(Long latestBarrierId) {
        return barrierRepository.findById(latestBarrierId).get();
    }

    @Override
    public Barrier findByGradeAndSubjectAndDegree(String grade,String subject,long degree){
        return barrierRepository.findByGradeAndSubjectAndDegree(grade,subject,degree);
    }

    @Override
    public boolean existsByGradeAndSubjectAndDegree(String grade,String subject,long degree){
        return  barrierRepository.existsByGradeAndSubjectAndDegree(grade,subject,degree);
    }

    @Override
    public boolean existsByGradeAndSubject(String grade, String subject){
        return  barrierRepository.existsByGradeAndSubject(grade,subject);
    }
    @Override
    public int addBarrier(Barrier barrier){
        Barrier barrierSave = barrierRepository.save(barrier);
        if(barrierSave != null)
            return StateCode.ADDSUCCESS;
        return StateCode.ADDFAIL;
    }
}
